package com.jianan.mis.app.basedata;

import org.apache.commons.lang.ArrayUtils;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.ListViewer;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.part.ViewPart;

import com.jianan.mis.data.domain.Material;
import com.jianan.mis.service.MaterialService;
import com.jianan.mis.sys.IMessageKeys;
import com.jianan.mis.sys.SpringBeanFactory;

public class MaterialView extends ViewPart {

	class ListLabelProvider extends LabelProvider {
		public String getText(Object element) {
			Material material = (Material) element;
			return material.getName();
		}
		public Image getImage(Object element) {
			return null;
		}
	}
	class ContentProvider implements IStructuredContentProvider {
		public Object[] getElements(Object inputElement) {
			return ((java.util.List) inputElement).toArray();
		}
		public void dispose() {
		}
		public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
		}
	}
	private Text txtCur;
	private Text txtNew;
	private List list;
	public static final String ID = "com.jianan.mis.app.basedata.MaterialView"; //$NON-NLS-1$

	private MaterialService materialService;
	
	/**
	 * Create contents of the view part
	 * @param parent
	 */
	@Override
	public void createPartControl(Composite parent) {
		
		if(!initializeServices()) {
			MessageDialog.openError(getViewSite().getShell(),
					IMessageKeys.OPEN_VIEW_FAIL, IMessageKeys.SERVICE_UNAVAILABLE);
			return;
		}
		
		//
		parent.setLayout(null);

		final Label label = new Label(parent, SWT.NONE);
		label.setText("现有材料类别:");
		label.setBounds(10, 10, 88, 20);

		final ListViewer materialListViewer = new ListViewer(parent, SWT.V_SCROLL | SWT.H_SCROLL | SWT.BORDER);
		materialListViewer.setLabelProvider(new ListLabelProvider());
		materialListViewer.addSelectionChangedListener(new ISelectionChangedListener() {
			public void selectionChanged(final SelectionChangedEvent event) {
				IStructuredSelection selection = (IStructuredSelection) event.getSelection();
				Material currentMaterial = (Material) selection.getFirstElement();
				if (currentMaterial != null)
					txtCur.setText(currentMaterial.getName());
			}
		});
		materialListViewer.setContentProvider(new ContentProvider());
		list = materialListViewer.getList();
		list.setBounds(20, 35, 204, 296);
		materialListViewer.setInput(materialService.getAll());

		final Label label_1 = new Label(parent, SWT.NONE);
		label_1.setText("新增材料类别:");
		label_1.setBounds(236, 10, 88, 12);

		txtNew = new Text(parent, SWT.BORDER);
		txtNew.setBounds(240, 37, 195, 25);

		final Button btnSave = new Button(parent, SWT.NONE);
		btnSave.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				if(exists(txtNew.getText())) {
					MessageDialog.openInformation(e.display.getActiveShell(), 
							IMessageKeys.FAIL, "材料类别名不可重复!");
					return;
				}
				Material material = new Material();
				material.setName(txtNew.getText());
				materialService.save(material);
				reset();
				materialListViewer.setInput(materialService.getAll());
				getViewSite().getActionBars().getStatusLineManager().setMessage(
					"材料类别已保存.");
			}
		});
		btnSave.setText("添加");
		btnSave.setBounds(240, 70, 58, 27);

		final Label label_2 = new Label(parent, SWT.NONE);
		label_2.setText("修改材料类别:");
		label_2.setBounds(235, 115, 88, 12);

		txtCur = new Text(parent, SWT.BORDER);
		txtCur.setBounds(241, 140, 195, 25);

		final Button btnUpdate = new Button(parent, SWT.NONE);
		btnUpdate.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				IStructuredSelection selection = (IStructuredSelection) materialListViewer.getSelection();
				if(selection.getFirstElement() != null) {
					Material currentMaterial = (Material) selection.getFirstElement();
					if(exists(txtCur.getText()) && !txtCur.getText().equals(currentMaterial.getName())) {
						MessageDialog.openInformation(e.display.getActiveShell(), 
								IMessageKeys.FAIL, "材料类别名不可重复!");
						return;
					}
					currentMaterial.setName(txtCur.getText());
					materialService.update(currentMaterial);
					reset();
					materialListViewer.setInput(materialService.getAll());
					getViewSite().getActionBars().getStatusLineManager().setMessage(
						"材料类别已更新.");
				} else {
					MessageDialog.openInformation(e.display.getActiveShell(), 
						IMessageKeys.FAIL, "请选择一个材料类别!");
				}
			}
		});
		btnUpdate.setText("修改");
		btnUpdate.setBounds(240, 175, 58, 27);

		final Label label_3 = new Label(parent, SWT.NONE);
		label_3.setText("删除材料类别:");
		label_3.setBounds(235, 220, 88, 12);

		final Button btnRemove = new Button(parent, SWT.NONE);
		btnRemove.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				IStructuredSelection selection = (IStructuredSelection) materialListViewer.getSelection();
				if(selection.getFirstElement() != null) {
					if(MessageDialog.openConfirm(e.display.getActiveShell(), 
							IMessageKeys.CONFIRM, "确认删除该材料类别吗?")) {
						Material currentMaterial = (Material) selection.getFirstElement();
						materialService.remove(currentMaterial);
						reset();
						materialListViewer.setInput(materialService.getAll());
						getViewSite().getActionBars().getStatusLineManager().setMessage(
							"材料类别已删除.");
					}
				} else {
					MessageDialog.openInformation(e.display.getActiveShell(), 
							IMessageKeys.FAIL, "请选择一个材料类别!");
				}
			}
		});
		btnRemove.setText("删除");
		btnRemove.setBounds(240, 240, 58, 27);
		
		createActions();
		initializeToolBar();
		initializeMenu();
	}
	
	private boolean initializeServices() {
		materialService = (MaterialService) SpringBeanFactory.getBean("materialService");
		if(materialService == null) {
			return false;
		}
		return true;
	}
	
	private void reset() {
		txtNew.setText("");
		txtCur.setText("");
	}
	
	private boolean exists(String name) {
		String[] materials = list.getItems();
		if(ArrayUtils.contains(materials, name))
			return true;
		else
			return false;
	}

	/**
	 * Create the actions
	 */
	private void createActions() {
		// Create the actions
	}

	/**
	 * Initialize the toolbar
	 */
	private void initializeToolBar() {
	}

	/**
	 * Initialize the menu
	 */
	private void initializeMenu() {
	}

	@Override
	public void setFocus() {
		// Set the focus
	}

}
